//
//  NSString+Category.m
//  ChartsDemo
//
//  Created by yunda on 2021/8/7.
//

#import "NSString+Category.h"

@implementation NSString (Category)

// 秒转时间
+ (NSString *)j_formatTimeFromSecond:(int)second{
    
    NSString *str_minute = [NSString stringWithFormat:@"%02d",(second%3600)/60 +(second/3600)*60];

    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02d",second%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
    return format_time;
}

// 时间转秒
+ (int)j_secondFromFormatTime:(NSString *)timeStr{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss"];
    [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
    NSTimeInterval time=(int )[[dateFormatter dateFromString:timeStr] timeIntervalSinceDate:[dateFormatter dateFromString:@"00:00"]];//间隔的秒数
    return time;
}

/// 字符串安全
+ (NSString*)j_safeString:(NSString*)string{
    if (string == nil) {
        return @"";
    }
    if (![string isKindOfClass:[NSString class]]) {
        return  [NSString stringWithFormat:@"%@",string];
    }
    if ([string isKindOfClass:[NSString class]]) {
        if ([string isEqualToString:@"null"] || [string isEqualToString:@"<null>"] || [string isKindOfClass:[NSNull class]]) {
            return @"";
        }
    }
    return string;
}

- (BOOL)j_isMobileNumber
{
    if ([self stringByReplacingOccurrencesOfString:@" " withString:@""].length != 11) {
        return NO;
    }
    NSString *regex = @"^(1[3-9]\\d{9}$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

- (BOOL)j_isNumber
{
    NSString *regex = @"^[0-9]*$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

+ (BOOL)j_hasChinese:(NSString *)sourceStr{
    NSString *emailRegex = @".*[\u4e00-\u9fa5]+.*";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:sourceStr];
}

#pragma mark - 简单计算textsize
- (CGSize)art_sizeWithLabelWidth:(CGFloat)width font:(UIFont *)font{
    NSDictionary *dict=@{NSFontAttributeName : font};
    CGRect rect=[self boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
    CGFloat sizeWidth=ceilf(CGRectGetWidth(rect));
    CGFloat sizeHieght=ceilf(CGRectGetHeight(rect));
    return CGSizeMake(sizeWidth, sizeHieght);
}

- (CGSize)art_sizeWithLabelWidth:(CGFloat)width font:(UIFont *)font maxHeight:(CGFloat)maxHeight{
    NSDictionary *dict=@{NSFontAttributeName : font};
    CGRect rect=[self boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
    CGFloat sizeWidth=ceilf(CGRectGetWidth(rect));
    CGFloat sizeHieght=ceilf(CGRectGetHeight(rect));
    return CGSizeMake(sizeWidth, MIN(maxHeight, sizeHieght));
}

- (CGSize)art_sizeWithLabelHeight:(CGFloat)height font:(UIFont *)font{
    NSDictionary *dict=@{NSFontAttributeName : font};
    CGRect rect=[self boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingTruncatesLastVisibleLine |
    NSStringDrawingUsesLineFragmentOrigin |
    NSStringDrawingUsesFontLeading attributes:dict context:nil];
    CGFloat sizeWidth=ceilf(CGRectGetWidth(rect));
    CGFloat sizeHieght=ceilf(CGRectGetHeight(rect));
    return CGSizeMake(sizeWidth, sizeHieght);
}

- (CGSize)art_sizeWithLabelHeight:(CGFloat)height font:(UIFont *)font maxWidth:(CGFloat)maxWidth {
    NSDictionary *dict=@{NSFontAttributeName : font};
    CGRect rect=[self boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
    CGFloat sizeWidth=ceilf(CGRectGetWidth(rect));
    CGFloat sizeHieght=ceilf(CGRectGetHeight(rect));
    return CGSizeMake(MIN(maxWidth, sizeWidth), sizeHieght);
}

/// 字符串判空
/// @param string <#string description#>
+ (BOOL)isBlankWithString:(NSString *)string{
    if ([string isEqualToString:@"(null)"]) {
        return YES;
    }
    if ([string isEqualToString:@"<null>"]) {
        return YES;
    }
    if (string.length == 0) {
        return YES;
    }
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if (![string isKindOfClass:[NSString class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

@end
